package com.hlm.transparent.core.resolve;

import com.hlm.transparent.model.TransparentData;
import com.hlm.transparent.util.ComUtil;
import com.hlm.transparent.util.HexUtil;
import com.hlm.transparent.util.ProtocolHelper;
import io.netty.buffer.ByteBuf;

import java.util.HashMap;
import java.util.Map;

/**
 * @program:transparent
 * @description:解析modbus
 * @author: xinY
 **/
public class ModBusResolve {
    public static TransparentData resolve(ByteBuf byteBuf, String protocolCode){
        Map<String,Object> resultMap = new HashMap<>();
        Map<String,Object> sectionMap = ProtocolHelper.getParameterMap(protocolCode,"command");
        if (sectionMap == null) {
            System.out.println("parse.ini不存在该协议");
            return null;
        }
        // 解析变量
        Object variableNum1 = sectionMap.get("variablenum"+(0+1));
        for (int i = 0; i < ComUtil.safeInt(variableNum1); i++) {
            String section = (0+1) + "_var" + i;
            Map<String,Object> map = ProtocolHelper.getParameterMap(protocolCode,section);
            assert map != null;
            int dataStart = ComUtil.safeInt( map.get("datastart"));
            int dataEnd = ComUtil.safeInt(map.get("dataend"));
            int dataOrder = ComUtil.safeInt(map.get("dataorder"));
            int dataType = ComUtil.safeInt(map.get("datatype"));
            int dataPoint = ComUtil.safeInt(map.get("datapoint"));
            int dataUnit = ComUtil.safeInt(map.get("dataunit"));
            String varType = ComUtil.safeString(map.get("vartype"));
            String unit = (String)map.get("unit");

            System.out.println("var:"+i+"\tstart:"+dataStart+"\tend:"+dataEnd+"\torder:"+dataOrder+"\ttype:"+dataType+"\tpoint:"+dataPoint+"\tunit:"+dataUnit+"\tunit"+unit+"\t");
            // 顺序  ： 直接拼接字符串
            if (dataOrder == 0 ){

                // BCD
                if ( 0 == dataType){
                    String var = "";
                    for (int j = dataStart ; j <= dataEnd ; j++){
                        var = var + HexUtil.getBCD(byteBuf.getByte(j));
                    }
                    resultMap.put(varType , var);
                }
                // hex
                else{
                    Double var = 0.0;
                    Double point = Math.pow(0.1, dataPoint);
                    int base = 1 ;
                    for (int j = dataEnd ; j >= dataStart ; j--){
                        var = var + byteBuf.getByte(j) * base;
                        base = base * 256;
                    }
                    resultMap.put(varType, (var * point));
                }
            }
            // 倒叙
            else{
                Double var = 0.0;
                // BCD
                if ( 0 == dataType){
                    // 时间,"0F"是 v15
                    if("0F".equals(varType)){
                        System.out.println("\"0F\".equals(varType）真，varType是"+varType);
                        String date = "";
                        for (int j = dataEnd ; j >= dataStart ; j --){
                            // 补零 拼接
                            date = date + ComUtil.fillZeroLeft(HexUtil.getBCD(byteBuf.getByte(j)),2) ;
                            resultMap.put(varType,date);
                        }
                    }
                    // v03 v04 v05 等
                    else{
                        System.out.println("\"0F\".equals(varType）假，varType是"+varType);
                        Double point = Math.pow(0.1, dataPoint);
                        for (int j = dataStart ; j <=dataEnd ; j ++){
                            var = var + HexUtil.getBCD(byteBuf.getByte(j)) * point ;
                            point = point * 100 ;
                        }
                        resultMap.put(varType, var);
                    }
                }
                // hex
                else{
                    Double point = Math.pow(0.1, dataPoint);
                    int base = 1 ;
                    for (int j = dataEnd ; j >= dataStart ; j --){
                        var = var + byteBuf.getByte(j) * base;
                        base = base * 256 ;
                    }
                    resultMap.put(varType , var * point);
                }
            }
        }
        // 解析state
        Map<String,Object> stateMap = ProtocolHelper.getParameterMap(protocolCode,"state");
        // TODO: 2020/8/19 解析state
        TransparentData transparentData = new TransparentData();
        transparentData.setDataMap(resultMap);
        for(String key : resultMap.keySet()){
            System.out.println(key+"：：：：：：：：：：：：：："+resultMap.get(key));
        }
        return transparentData;
    }
}
